#include <stdio.h>
#include <string.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


int main(int argv, char *argc[])
{
	struct stat buf;
	struct passwd *uidInfo;
	struct group *gidInfo;
	
	if(argv < 2)
	{
		printf("parameter error\r\n");
		return -1;
	}
	
	//获取文件属性
	if(lstat(argc[1], &buf) == -1) 
	{
		perror("lstat");
		return -1;
	}
	
	//提取文件类型
	switch(buf.st_mode & S_IFMT)
	{
		case S_IFSOCK:
			printf("s");
			break;
		case S_IFLNK:
			printf("l");
			break;
		case S_IFREG:
			printf("-");
			break;
		case S_IFBLK:
			printf("b");
			break;
		case S_IFDIR:
			printf("d");
			break;
		case S_IFCHR:
			printf("c");
			break;
		case S_IFIFO:
			printf("p");
			break;
	}
	//提取文件权限
	if(buf.st_mode & S_IRUSR)
		printf("r");
	else
		printf("-");
	if(buf.st_mode & S_IWUSR)
		printf("w");
	else
		printf("-");
	if(buf.st_mode & S_IXUSR)
		printf("x");
	else
		printf("-");
		
	if(buf.st_mode & S_IRGRP)
		printf("r");
	else
		printf("-");
	if(buf.st_mode & S_IWGRP)
		printf("w");
	else
		printf("-");
	if(buf.st_mode & S_IXGRP)
		printf("x");
	else
		printf("-");
		
	if(buf.st_mode & S_IROTH)
		printf("r");
	else
		printf("-");
	if(buf.st_mode & S_IWOTH)
		printf("w");
	else
		printf("-");
	if(buf.st_mode & S_IXOTH)
		printf("x");
	else
		printf("-");
	printf("\t");
	
	//获取链接数
	printf("%ld", buf.st_nlink);
	printf("\t");
	
	//获取所属者
	uidInfo = getpwuid(buf.st_uid);
	if(uidInfo != NULL)
	{
		printf("%s", uidInfo->pw_name);
		printf("\t");
	}
	
	//获取所属组
	gidInfo = getgrgid(buf.st_gid);
	if(gidInfo != NULL)
	{
		printf("%s", gidInfo->gr_name);
		printf("\t");
	}
	
	//获取大小
	printf("%ld", buf.st_size);
	printf("\t");
	
	//获取上次状态改变的时间
	printf("%s", ctime(&buf.st_ctime));
	return 0;
}